package com.wtb.javatool.service.impl;

import com.wtb.javatool.dao.ProjectClassMapper;
import com.wtb.javatool.dto.ClassDto;
import com.wtb.javatool.entity.ProjectClass;
import com.wtb.javatool.service.ProjectClassService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectClassServiceImpl implements ProjectClassService {
    private final ProjectClassMapper projectClassMapper;

    public ProjectClassServiceImpl(ProjectClassMapper projectClassMapper) {
        this.projectClassMapper = projectClassMapper;
    }

    @Override
    public List<ClassDto> findProjectClassesAndTeamsByCourseId(Long courseId) {
        return projectClassMapper.findProjectClassesAndTeamsByCourseId(courseId);
    }

    @Override
    public void addProjectClass(Long courseId, String className) {
        ProjectClass projectClass = new ProjectClass();
        projectClass.setClassName(className);
        projectClassMapper.insertClass(projectClass);
        projectClassMapper.insertCourseClassRef(courseId, projectClass.getClassId());
    }

    @Override
    public void deleteProjectClass(Long classId) {
        projectClassMapper.deleteCourseClassRef(classId);
    }

    @Override
    public void insertClassStudentRef(Long classId, List<Long> studentIds) {
        if (classId == null || studentIds == null || studentIds.isEmpty()) {
            System.out.println("无法添加学生到班级：参数无效，classId=" + classId + ", studentIds=" + (studentIds == null ? "null" : studentIds.size()));
            return;
        }
        
        System.out.println("添加学生到班级: classId=" + classId + ", 学生数量=" + studentIds.size());
        
        try {
            projectClassMapper.insertClassStudentRef(classId, studentIds);
            System.out.println("成功添加" + studentIds.size() + "名学生到班级" + classId);
        } catch (Exception e) {
            System.err.println("添加学生到班级失败: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void deleteClassStudentRef(Long classId, List<Long> studentIds) {
        projectClassMapper.deleteClassStudentRef(classId,studentIds);
    }


}
